﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Tenencias;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Tenencias
{
	public partial class TenenciasViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Tenencia> _tenencias;

		/// <summary>
		/// 
		/// </summary>
		private Tenencia _tenencia;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Tenencia> Tenencias
		{
			get
			{
				return _tenencias;
			}
			private set
			{
				_tenencias = value;
				if (_tenencias != null)
					RaisePropertyChanged("Tenencias");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Tenencia Tenencia
		{
			get
			{
				return _tenencia;
			}
			private set
			{
				_tenencia = value;
				if (_tenencia != null)
					RaisePropertyChanged("Tenencia");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadTenencias()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					ITenenciaService service = IoCFactory.Resolve<ITenenciaService>();
					e.Result = service.FindAllTenencias();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Tenencias = e.Result as List<Tenencia>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewTenencia(object sender, EventArgs e)
		{
			_tenenciasView.tenenciasBindingSource.AddingNew += AddingNewTenencia;
			_tenenciasView.tenenciasBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewTenencia(object sender, AddingNewEventArgs e)
		{
			Tenencia = new Tenencia
			{
				Nombre = string.Empty,
				Status = true,
				TenenciaGuid = Guid.NewGuid(),
				TenenciaId = 0,
			};
			Tenencia.MarkAsAdded();
			e.NewObject = Tenencia;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveTenencia(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					ITenenciaService service = IoCFactory.Resolve<ITenenciaService>();
					Tenencias.ForEach(t =>
					{
						if (t.ChangeTracker.State == ObjectState.Added)
							service.AddTenencia(t);
						else
							service.ChangeTenencia(t);
						t.AcceptChanges();
					});
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					EndWaiting();
					if (args.Error != null)
						ShowMessage(args.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentTenencia(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				Tenencia = bindingSource.Current as Tenencia;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnTenenciasPropertyChanged()
		{
			_tenenciasView.tenenciasBindingSource.DataSource = Tenencias;
			_tenenciasView.tenenciasBindingSource.CurrentChanged += SetCurrentTenencia;
			SetCurrentTenencia(_tenenciasView.tenenciasBindingSource, new EventArgs());
		}

		private void OnTenenciaPropertyChanged()
		{
		}

		#endregion

	}
}